package varredor;

import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.List;

public class IdentificadorRede {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		NetworkInterface rede = null;
		try {

			Enumeration<NetworkInterface> netWorks = NetworkInterface.getNetworkInterfaces();

			while (netWorks.hasMoreElements()) {
				NetworkInterface networkInterface = netWorks.nextElement();

				System.out.println(networkInterface.getName() + "  -  " + networkInterface.getDisplayName());

				List<InterfaceAddress> enumInterface = networkInterface.getInterfaceAddresses();
				for (InterfaceAddress interfaceAddress : enumInterface) {

					System.out.println("IP: " + interfaceAddress.getAddress());
					System.out.println("Broad: " + interfaceAddress.getBroadcast());
				}
				if (networkInterface.getHardwareAddress() != null) {
					System.out.print("MAC: ");
					for (byte b : networkInterface.getHardwareAddress()) {
						System.out.print(Integer.toHexString(b).replaceAll("ffffff", "-").toUpperCase());
					}
					System.out.print("\n");
				}

			}

			rede = NetworkInterface.getByName("eth1");

			if (true)
				return;
			//rede = NetworkInterface.getByName("eth0");

		} catch (SocketException erro) {
			System.out.println("erro");
		}

		List<InterfaceAddress> listaInterface = rede.getInterfaceAddresses();

		for (int i = 0; i < listaInterface.size(); i++) {
			System.out.println("Broadcast " + listaInterface.get(i).getBroadcast());
			System.out.println("Endereco IP " + listaInterface.get(i).getAddress());
			System.out.println("Endereco Mascara subRede " + listaInterface.get(i).getNetworkPrefixLength() + "\n");
			//Valores típicos de IPv4 seria 8 (255.0.0.0), 16 (255.255.0.0) ou 24 (255.255.255.0).
			//Os valores típicos IPv6 seria 128 (:: 1/128) ou 10 (fe80 :: 203: baff: fe27: 1243/10)
			if (i == 1) {
				System.out.println("Usando o IP de broadcast " + listaInterface.get(i).getBroadcast());

				//Usuario usuario = new Usuario(InetAddress.getByName("192.168.43.255"));
			}
			//Usuario usuario = new Usuario(interfaceAddress.getBroadcast());

		}
	}
}
